<?php

namespace App\Http\Controllers\Innerend;


use App\Models\Admin;
use App\Models\SiteInfo;

class UserController extends BaseController
{

    // 用户
    public function initialize()
    {
        parent::initialize();

        $this->model = 'User';
    }


    // 列表
    // 添加
    // 修改
    // 删除
    // 启用、禁用

    // 修改密码
    public function onPassword()
    {
        $manage = $this->checkManage($this->model);
        if($manage === false){
            return $this->error($manage->getError());
        }

        $result = $manage->onPassword();

        return $this->success($result);
    }


    // 修改密码 页面
    public function password()
    {
        $manage = $this->checkManage($this->model);
        if($manage === false){
            return $this->error($manage->getError());
        }

        $html = '';
        $info = $manage->onShow();
        $form = $this->adminForm();

        // 用户名
        $username = $form->formInput('username', $info['username'] ?? '', '用户名');
        $nickname = $form->formInput('nickname', $info['nickname'] ?? '', '昵称');
        $password = $form->formPassword('password', '', '密码');
        $_id      = $form->formHidden('id', $info['id'] ?? '');

        //$html     .= $form->makeItem('确认密码', $password, '添加时候不填默认密码123456，修改不填默认不修改');
        $html .= $form->makeItem('用户名', $username);
        $html .= $form->makeItem('昵称', $nickname);
        $html .= $form->makeItem('密码', $password, '请输入密码');
        $html .= $form->makeItem('确认密码', $password, '请再次输入密码');
        $html .= $_id;


        return $this->dispaly('form', ['formHtml' => $html]);
    }

    // 列表数据展示
    protected function listTable($lists)
    {
        // 整理
        $trList = [];
        if(empty($lists)){
            return $trList;
        }

        // 展示列表表格
        $sexes    = [1 => '男', 2 => '女', 0 => '保密'];
        $listHtml = $this->adminList();
        foreach($lists as $key => $item){
            // 数据
            $_id      = $item['id'];
            $status   = $item['status'] == 1 ? '正常' : '禁用';
            $statusOp = $item['status'] == 1 ? '禁用' : '启用';
            // 操作
            $operate = $listHtml->operate_status(['id' => $_id, 'value' => 1 - $item['status']], $statusOp, $item['status']);
            $operate .= $listHtml->operate_edit(['id' => $_id], '编辑用户');
            $operate .= $listHtml->operate_soft_delete(['id' => $_id], '你确定要删除该用户吗？');

            // 页面
            $tr = $listHtml->list_check($item['id']);
            $tr .= $listHtml->list_td($item['nickname']);
            $tr .= $listHtml->list_image($item['head_img']);
            $tr .= $listHtml->list_td($sexes[$item['sex']] ?? '-');
            $tr .= $listHtml->list_td($item['mobile']);
            $tr .= $listHtml->list_td($item['is_auth'] == 1 ? '是' : '否');
            $tr .= $listHtml->list_td($status);
            $tr .= $listHtml->list_td(date('Y-m-d H:i', $item['ctime']));
            $tr .= $listHtml->list_td($operate);

            // 整合
            $trList[] = $listHtml->list_tr($tr);
        }

        // 整合
        return $trList;
    }

    // 列表顶部按钮
    protected function listTopButton()
    {
        $addUrl     = Url('/innerend/user/add');
        $top_button = '<a href="javascript:;" onclick="onEdit(\''.$addUrl.'\')" class="btn btn-success radius">添加用户</a> ';
        $this->assign('top_button', $top_button);
    }

    // 列表字段
    protected function list_fields()
    {
        return ['nickname' => '昵称', 'head_img' => '头像', 'sex' => '性别', 'mobile' => '手机号', 'is_auth' => '是否认证', 'status' => '状态', 'ctime' => '创建时间', 'operate' => '操作',];
    }

    // 列表搜索
    protected function list_search()
    {
        $request  = $this->optLogic()->getData();
        $form     = $this->adminList();
        $nickname = $form->formInput('nickname', $request['nickname'] ?? '', '昵称');
        $mobile   = $form->formInput('mobile', $request['mobile'] ?? '', '手机号');
        $search   = ['nickname' => $nickname, 'mobile' => $mobile];  // 'username' => $username,
        $this->assign('search', $search);

        $searchHide = ['channel'];
        $this->assign('searchHide', []);
    }

    // 表单页面
    protected function formTable()
    {
        $manage = $this->checkManage($this->model);
        if($manage === false){
            return $this->error($manage->getError());
        }

        $html = '';
        $info = $manage->onShow();
        $form = $this->adminForm();

        $adminDb = new Admin();
        $all     = $adminDb->getRole();

        // 用户名
        $status   = $form->formRadio('status', $info['status'] ?? 1, [1 => '正常', 0 => '禁用']);
        $username = $form->formInput('username', $info['username'] ?? '', '用户名');
        $nickname = $form->formInput('nickname', $info['nickname'] ?? '', '昵称');
        $head_img = $form->formImage('head_img', $info['head_img'] ?? '');
        $mobile   = $form->formInput('mobile', $info['mobile'] ?? '', '手机号');
        $email    = $form->formInput('email', $info['email'] ?? '', '邮箱');
        $sign     = $form->formInput('sign', $info['sign'] ?? '', '签名');
        $sex      = $form->formRadio('sex', $info['sex'] ?? 1, [1 => '男', 2 => '女', 0 => '保密']);
        $password = $form->formPassword('password', '', '密码');
        $_id      = $form->formHidden('id', $info['id'] ?? '');

        //$html     .= $form->makeItem('确认密码', $password, '添加时候不填默认密码123456，修改不填默认不修改');
        $html .= $form->makeItem('用户名', $username);
        $html .= $form->makeItem('昵称', $nickname);
        $html .= $form->makeItem('性别', $sex);
        $html .= $form->makeItem('头像', $head_img);
        $html .= $form->makeItem('手机号', $mobile);
        $html .= $form->makeItem('邮箱', $email);
        $html .= $form->makeItem('签名', $sign);
        $html .= $form->makeItem('密码', $password, '添加时候不填默认密码123456，修改不填默认不修改');
        $html .= $form->makeItem('状态', $status);
        $html .= $_id;

        return $html;
    }

}


